﻿using Microsoft.Extensions.Configuration;
using System;
using System.Reflection;
namespace FileConvert
{
    class Program
    {
        /// <summary>
        /// 在linux 下面运行必须含有libskiasharp文件
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            //var section= AppConfig.configuration.GetSection("VideoConvertTools"); 
            //Console.WriteLine(section.GetSection("ffmpeg").Value);
            //Console.WriteLine(section.GetSection("ffprobe").Value);
            //Console.ReadLine();
            string inputFile = @"";
            string outputFile = @"";
            foreach (var item in args)
            {
                if (item.IndexOf("-i") == 0) //输入的文件路径
                {
                    inputFile = item.Substring(2, item.Length - 2);
                    //Console.WriteLine(input);
                }
                else if (item.IndexOf("-o") == 0) //输入的文件路径
                {
                    outputFile = item.Substring(2, item.Length - 2);
                }
            }
            //#endif

            if (System.IO.File.Exists(inputFile) == false)
            {
                Console.WriteLine(string.Format("{0};{1}", 0, "输入的文件不存在"));
                return;
            }

          //  Console.WriteLine("输入文件的路径是"+System.IO.Path.GetFullPath(inputFile));
            //判断传入的是文件还是文件夹
            string dir = "";
            string ext = System.IO.Path.GetExtension(outputFile).Trim();
            if (string.IsNullOrEmpty(ext) == false)
            {
                dir = System.IO.Path.GetDirectoryName(outputFile);
                if (System.IO.File.Exists(outputFile))
                {
                    try
                    {
                        System.IO.File.Delete(outputFile);
                    }
                    catch
                    {

                        Console.WriteLine(string.Format("{0};{1}", 0, "目标文件已被占用,并且无法进行删除"));
                        return;
                    }
                }
            }
            else
            {
                ext = System.IO.Path.GetFileNameWithoutExtension(outputFile); //取文件的名称
                dir = outputFile;
            }
            if (System.IO.Directory.Exists(dir) == false)
            {
                //Console.WriteLine(dir);
                try
                {
                    System.IO.Directory.CreateDirectory(dir);
                }
                catch(Exception ex)
                {
                    Console.WriteLine(string.Format("{0};{1}", 0, "创建输出目录失败,请更换文件的输出目录:"+ex.Message));
                    return;
                }
            }
            string dealClassName = ext.Replace(".", "").ToLower() + "convert";
            //进行文件的转换

            Assembly assembly = Assembly.GetExecutingAssembly();
            Type[] types = assembly.GetTypes();
            IConvert c = null;
            foreach (Type type in types)
            {
                if (type.GetInterface(typeof(IConvert).ToString()) != null && !type.IsAbstract)
                {
                    if (type.Name.ToLower() == dealClassName)
                    {
                        c = (IConvert)type.GetConstructor(new Type[] { typeof(string), typeof(string) }).Invoke(new Object[] { inputFile, outputFile });
                    }
                }
            }
            if (c != null)
            {
                try
                {
                    bool b = c.Convert();
                    Console.WriteLine(string.Format("{0};{1}", b == true ? 1 : 0, b == true ? "转换成功" : "转换失败"));
                    return;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(string.Format("{0};{1}", 0,"异常信息:"+ ex.Message));
                }
            }
            else
            {
                Console.WriteLine(string.Format("{0};{1}", 0, "转换失败"));
                return;
            }
        } 
    }
}
